package com.web.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;

@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter(){

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        // 允许发送Cookie
        config.setAllowCredentials(true);
        // 允许所有来源的请求
        config.addAllowedOriginPattern("*");
        // 允许所有的请求头字段
        config.addAllowedHeader("*");
        // 允许所有的请求方法（GET, POST, PUT, DELETE等）
        config.addAllowedMethod("*");
        // 对所有的路径都应用跨域配置
        source.registerCorsConfiguration("/**", config);
        // 创建并返回一个跨域过滤器实例
        return new CorsFilter(source);
    }

}